package com.weigee.ktv.presenter.adapter.rv

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.linde.library.adapter.BaseRvAdapter
import com.linde.library.adapter.SimpleViewHolder
import com.weigee.ktv.R
import com.weigee.ktv.model.EventTag
import com.weigee.ktv.model.entities.SupermarketShoppingCategoryEntity
import com.weigee.ktv.model.interfaces.IShoppingCart
import kotlinx.android.synthetic.main.adapter_supermarket_shopping_category.view.*

/**
 * Created by linde on 2017/5/28.
 *<p/>
 */
class SupermarketShoppingCategoryAdapter(val cart: IShoppingCart) : BaseRvAdapter<SupermarketShoppingCategoryEntity>()
{
//    val list = ArrayList<SupermarketShoppingCategoryEntity>()
    var selectedItem: Int = 0

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): SimpleViewHolder
    {
        val view = LayoutInflater.from(parent?.context).inflate(R.layout.adapter_supermarket_shopping_category, parent, false)
        return SimpleViewHolder(view)
    }

    override fun onBindViewHolder(holder: SimpleViewHolder?, position: Int)
    {
        holder ?: return
        val entity: SupermarketShoppingCategoryEntity? = mDataList[position]
        val view = holder.itemView
        val isSelected = selectedItem == position

        view.tv_category.text = entity?.name
        // 选中状态
        if (isSelected)
        {
            view.iv_check.visibility = View.VISIBLE
            view.item_bg.setBackgroundResource(R.color.skinAdapterSupermarketShoppingCategory_backgroundChecked)
        }
        else
        {
            view.iv_check.visibility = View.GONE
            view.item_bg.setBackgroundResource(R.color.skinAdapterSupermarketShoppingCategory_background)
        }
        // 购物车参数
        val cartNum = cart.getShoppingCartNum(null, entity?.id, null)
        if (cartNum > 0)
        {
            view.frame_tip_num.visibility = View.VISIBLE
            view.tv_check_num.text = cartNum.toString()
        }
        else
        {
            view.frame_tip_num.visibility = View.GONE
        }
        // 点击商品分类事件->刷新商品列表
        view.setOnClickListener({
            selectedItem = position
            com.linde.library.eventbus.EventSend(EventTag.eventSelectedShoppingCategory, entity?.id).send()
            notifyDataSetChanged()
        })
    }
}